﻿using System;
using System.Linq;
using System.Reflection;
using Microsoft.EntityFrameworkCore;

namespace Competition.Domain
{
    public class AppDbContext : DbContext, IDbContext
    {
        public AppDbContext(DbContextOptions<AppDbContext> options) : base((DbContextOptions)options)
        {

        }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseSqlServer(
                    @"Database=business;Data Source=47.98.142.148;Port=3306;User Id=root;Password=123456;Charset=utf8;TreatTinyAsBoolean=false;");
            }
        }
        protected override void OnModelCreating(ModelBuilder builder)
        {
            Type type = new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().DeclaringType;  //当前类型
            Assembly assembyle = Assembly.GetAssembly(type);  //查找类库
            var typeList = assembyle.GetTypes().Where(t => ((TypeInfo)t).ImplementedInterfaces.Contains(typeof(IAggregateRoot)));  //查找该命名空间下实现了接口的所有类型
            foreach (Type type1 in typeList)
            {
                builder.Entity(type1);
            }

            base.OnModelCreating(builder);
        }

    }
}